<?php

namespace app\common\service;

use app\common\service\Base;
use app\common\service\MaterialStockUse as MaterialStockUseService;

class MaterialStock extends Base
{
	/**
	 * 获取单条信息
	 *
	 * @author KING
	 * @date 2020年7月15日
	 *
	 * @param integer $materialStockId
	 * @return array
	 */
	public function getInfoById($materialStockId) {
		if (empty($materialStockId)) return [];
		return db('material_stock')->where('id', $materialStockId)->findOrEmpty();
	}

	/**
	 * 获取某个材料余料列表及占用情况
	 *
	 * @author KING
	 * @date 2020年7月15日
	 *
	 * @param integer $materialId
	 * @param integer $orderId
	 * @return array
	 */
	public function getListByMaterialIdOrderId($materialId, $orderId,$materialtype='') {
		$result = [];
		$materialStockUseService = new MaterialStockUseService();
		$applyNum = $usedNum = 0;
		$where = [
			['material_id', '=', $materialId],
			['status', '=', 1],
		];
		if($materialtype=='private'){ //私有仓余料
            $factory_id=db('order')->where('id', $orderId)->value('factory_id', '');
            $where[]=['factory_id','=',$factory_id];
            $where[]=['audit_status','=',1];
            $where[]=['cutting_time','>',0];
        }else{ //公共仓余料
            $where[]=['cutting_time','=',0];
        }
        $storehouseKV = getStorehouseKV();
        $storeshelfKV = getStoreshelfKV(0);
		$list = db('material_stock')->where($where)->order("id asc")->select();
		foreach ($list as $k => $v) {
			// 获取占用数
			$list[$k]['apply_num'] = $materialStockUseService->getApplyNum($v['id'], $orderId);
			if($list[$k]['apply_num']<=0 && $v['metre_left']<=0){ //去除不是当前订单占用,且库存为0的余料
			    unset($list[$k]);continue;
            }
			$applyNum += $list[$k]['apply_num'];

			// 获取使用数
			$list[$k]['used_num'] = $materialStockUseService->getUsedNum($v['id'], $orderId);
			$usedNum += $list[$k]['used_num'];
            $list[$k]['storehouse_name'] = arrayGet($storehouseKV, $v['storehouse_id'], '');
            $list[$k]['storeshelf_name'] = arrayGet($storeshelfKV, $v['storeshelf_id'], '');
		}
		$result['applyNum'] = $applyNum;
		$result['usedNum'] = $usedNum;
		$result['list'] = $list;
		return $result;
	}

	/**
	 * 检测是否有可用的库存
	 *
	 * @author KING
	 * @date 2020年3月25日
	 *
	 * @param int $materialId
	 *
	 */
	public function checkMetreLeft($materialId) {
		if(!$materialId) return false;
		$where = [
			['material_id', '=', $materialId],
			['metre_left', '>', 0]
		];
		return db('material_stock')->where($where)->count()?true:false;
	}

    /**
     * 检测普通仓库可用余料信息
     */
	public function checkPrivateMetres($materialId,$orderId) {
        if(!$materialId) return false;
        $factory_id=db('order')->where('id', $orderId)->value('factory_id', '');
        $where = [
            ['factory_id', '=', $factory_id],
            ['material_id', '=', $materialId],
            ['audit_status','=',1],
            ['metre_left', '>', 0],
            ['cutting_time','>',0]
        ];
        $have_metres= db('material_stock')->where($where)->sum('metre_left');
        $where = [
            ['stock_use.material_id', '=', $materialId],
            ['stock_use.order_id', '=', $orderId],
            ['stock.cutting_time','>',0]
        ];
        $useMetres= db('material_stock_use stock_use')->join('material_stock stock', "stock.id=stock_use.material_stock_id", 'inner')
            ->where($where)->where('stock_use.status', 1)->sum('stock_use.metre');
        $retdata['have_metres']=$have_metres;
        $retdata['used_metres']=$useMetres;
        return $retdata;
    }

	/**
	 * 根据材料ID获取余料
	 *
	 * @author KING
	 * @date 2020年3月31日
	 *
	 */
	public function getMetreLeft($materialId) {
		if(!$materialId) return 0;
		$where = [
			['material_id', '=', $materialId],
		];
		return db('material_stock')->where($where)->sum('metre_left');
	}
}